package com.fengmoxi.ctv;

import static android.os.Build.VERSION.SDK_INT;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.util.Log;

import com.fengmoxi.ctv.manage.AppInstalledReceiver;
import com.fengmoxi.ctv.manage.HdmiInReceiver;

public class MyApp extends Application {
    private static final String TAG = MyApp.class.getSimpleName();
    private AppInstalledReceiver mAppInstalledReceiver;
    private HdmiInReceiver mHdmiInReceiver;

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");
        IntentFilter filter_app = new IntentFilter();
        filter_app.addDataScheme("package");
        filter_app.addAction(Intent.ACTION_PACKAGE_ADDED);//应用安装的广播
        filter_app.addAction(Intent.ACTION_PACKAGE_REPLACED);//应用替换的广播
        filter_app.addAction(Intent.ACTION_PACKAGE_REMOVED);//应用卸载的广播
        mAppInstalledReceiver = new AppInstalledReceiver();
        registerReceiver(mAppInstalledReceiver, filter_app);
        IntentFilter filter_hdmi_in = new IntentFilter();
        filter_hdmi_in.addAction("android.intent.action.HDMI_IN");
        mHdmiInReceiver = new HdmiInReceiver();
        if (SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            registerReceiver(mHdmiInReceiver, filter_hdmi_in, RECEIVER_EXPORTED);
        } else {
            registerReceiver(mHdmiInReceiver, filter_hdmi_in);
        }
    }

    public void onTerminate() {
        super.onTerminate();
        Log.i(TAG, "onTerminate");
        if (mAppInstalledReceiver != null) {
            unregisterReceiver(mAppInstalledReceiver);
        }
        if (mHdmiInReceiver != null) {
            unregisterReceiver(mHdmiInReceiver);
        }
    }
}
